package com.example.shopping.service;


import com.example.shopping.entity.Admin;
import com.example.shopping.entity.User;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class
AdminServiceProxy implements AdminService {


    @Resource(name = "adminserviceimpl")
    private AdminService adminService;
    @Autowired
    private LogService logService;

    @Override
    public Admin login(String account, String password) {
        System.out.println("代理者模式：管理者登录");
        //1. 执行真实业务
        Admin admin = adminService.login(account, password);
        if (admin != null) {
            //日志
            logService.logOperation("admin", "登录", "", true, "");
            return admin;
        } else {
            return null;
        }
    }

    @Override
    public List<User> getAllUser() {
        //日志
        logService.logOperation("admin", "获取所有用户", "", true, "");
        return adminService.getAllUser();
    }

    @Override
    public List<User> getAllUserByPage(int page, int pageSize) {
        try {
            //日志
            logService.logOperation("admin", "获取所有用户", "", true, "");
            return adminService.getAllUserByPage(page, pageSize);
        } catch (Exception e) {
            //日志
            logService.logOperation("admin", "获取所有用户", "", false, e.getMessage());
            return null;
        }
    }

    @Override
    public long count() {
        try {
            //日志
            logService.logOperation("admin", "获取所有用户数量", "", true, "");
            return adminService.count();
        } catch (Exception e) {
            //日志
            logService.logOperation("admin", "获取所有用户数量", "", false, e.getMessage());
            return 0;
        }
    }

    @Override
    public boolean updateAdminPassword(String account, String oldPassword, String newPassword) {
        try {
            // 日志记录
            logService.logOperation("admin", "修改管理员密码", "", true, "");
            return adminService.updateAdminPassword(account, oldPassword, newPassword);
        } catch (Exception e) {
            // 日志记录
            logService.logOperation("admin", "修改管理员密码", "", false, e.getMessage());
            return false;
        }
    }

}

